package com.dreamwhale.machinery.goods.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dreamwhale.machinery.dao.DriveTypesMapper;
import com.dreamwhale.machinery.entity.DriveTypes;
import com.dreamwhale.machinery.goods.entity.DriverInfo;
import com.dreamwhale.machinery.goods.entity.DriverWorkInfo;
import com.dreamwhale.machinery.goods.mapper.DriverWorkInfoMapper;
import com.dreamwhale.machinery.goods.service.DriverWorkInfoService;
import com.dreamwhale.machinery.user.entity.YxUser;
import com.dreamwhale.machinery.user.mapper.YxUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 司机找工作服务实现类
 * </p>
 *
 * @author Author
 * @since 2020-05-28
 */
@Service
public class DriverWorkInfoServiceImpl extends ServiceImpl<DriverWorkInfoMapper, DriverWorkInfo> implements DriverWorkInfoService {

    @Resource
    private DriverWorkInfoMapper driverWorkInfoMapper;

    @Resource
    private YxUserMapper yxUserMapper;

    @Resource
    private DriveTypesMapper driveTypesMapper;

    @Override
    public int insertDriverWorkInfos(DriverWorkInfo driverWorkInfo) {
        driverWorkInfo.setCreateTime(LocalDateTime.now());
        return driverWorkInfoMapper.insert(driverWorkInfo);
    }

    @Override
    public List<DriverWorkInfo> selectDriverWorkInfoMap(Integer userId) {
        QueryWrapper<DriverWorkInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("del_flag", 0);
        return driverWorkInfoMapper.selectList(queryWrapper);
    }

    @Override
    public List<DriverWorkInfo> selectDriverWorkInfos(Integer infoId) {
        QueryWrapper<DriverWorkInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0).eq("id", infoId);
        List<DriverWorkInfo> list = driverWorkInfoMapper.selectList(queryWrapper);
        if (list != null && list.size() != 0) {
            for (DriverWorkInfo driverWorkInfo : list) {
                QueryWrapper<DriveTypes> queryWrappers = new QueryWrapper<>();
                queryWrappers.eq("del_flag", 0).eq("id", driverWorkInfo.getDriveTypeId());
                DriveTypes driveTypes = driveTypesMapper.selectOne(queryWrappers);
                driverWorkInfo.setDriveTypeName(driveTypes.getDriveType());
            }
        }
        return list;
    }

    @Override
    public DriverWorkInfo selectDriverWorkInfoByTypeId(Integer infoId) {
        return driverWorkInfoMapper.selectDriverWorkInfoByTypeId(infoId);
    }

    @Override
    public List<DriverWorkInfo> selectDriverWorkInfoHome(Integer salaryMin, Integer salaryMax, Integer years, String province, String city,
                                                         Integer type, Integer types, String title, String driveTypeId) {
        //找工作
        List<DriverWorkInfo> driverWorkInfo = driverWorkInfoMapper.selectDriverWorkInfoHome(salaryMin, salaryMax, years, province, city, type, types, title, driveTypeId);
        if (driverWorkInfo != null) {
            for (DriverWorkInfo machineBuy1 : driverWorkInfo) {
                //查询用户信息
                QueryWrapper<YxUser> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("uid", machineBuy1.getUserId());
                YxUser listy = yxUserMapper.selectOne(queryWrapper);
                machineBuy1.setNickname(listy.getNickname());
                machineBuy1.setAvatar(listy.getAvatar());
                QueryWrapper<DriveTypes> queryWrappers = new QueryWrapper<>();
                queryWrappers.eq("del_flag", 0).eq("drive_type", machineBuy1.getDriveTypeId());
                DriveTypes driveTypes = driveTypesMapper.selectOne(queryWrappers);
                machineBuy1.setDriveTypeName(driveTypes.getDriveType());
            }
        }
        return driverWorkInfo;
    }
}
